package com.yingjie.addressbook;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;

public class Group {
	
	static void addContactToMultiGroup(Activity activity, ArrayList<ContentProviderOperation> ops,int contactId,String strGroupIds,boolean isNewContact){
		if(strGroupIds == "") return;
		String strGroupArray[] = strGroupIds.split(",");
		ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
				.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND " +  Data.MIMETYPE + "=?", new String[]{String.valueOf(contactId),GroupMembership.CONTENT_ITEM_TYPE})
				.build());
		for(int i = 0;i < strGroupArray.length; i ++){
			addContactToGroup(activity,ops,contactId,Integer.parseInt(strGroupArray[i]),isNewContact);
		}
	}
	
    private static void addContactToGroup(Activity activity,ArrayList<ContentProviderOperation> ops, int contactId,int groupId,boolean isNewContact) {     
    	if(isNewContact){
  			 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
			          .withValueBackReference(Data.RAW_CONTACT_ID, contactId)
			          .withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
			          .withValue(GroupMembership.GROUP_ROW_ID,groupId)
			          .build());
    	}else{
   			 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
			          .withValue(Data.RAW_CONTACT_ID, contactId)
			          .withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
			          .withValue(GroupMembership.GROUP_ROW_ID,groupId)
			          .build());
    	}
}
	
    private static boolean existContactInGroup(Activity activity, int contactId, int groupId) {
        String where = Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
                        + "' AND " + Data.DATA1 + " = '" + groupId
                        + "' AND " + Data.RAW_CONTACT_ID + " = '" + contactId + "'";
        Cursor markCursor = activity.getContentResolver().query(Data.CONTENT_URI, new String[]{Data.DISPLAY_NAME}, where, null, null);
        if (markCursor.moveToFirst()) {
                return true;
        }else {
                return false;
        }
    }
    public static String getContactGroup(Activity activity, String strContactId) {
        String where = Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
                + "' AND " + Data.RAW_CONTACT_ID + " = '" + strContactId + "'";
        Cursor cursor = activity.getContentResolver().query(Data.CONTENT_URI, null, where, null, null);
        String strReturn = "";
        while (cursor.moveToNext())   
        {  
        	int iGroupColumnId = cursor.getColumnIndex(Data.DATA1);
        	int iGroupId = cursor.getInt(iGroupColumnId);
        	String strGroupId = String.valueOf(iGroupId);
        	String strGroupName =getGroupNameById(activity, strGroupId);
        	strReturn += (strGroupName +",");
        }
        return strReturn;
    }
    
    public static String getContactGroupId(Activity activity, String strContactId) {
        String where = Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
                + "' AND " + Data.RAW_CONTACT_ID + " = '" + strContactId + "'";
        Cursor cursor = activity.getContentResolver().query(Data.CONTENT_URI, null, where, null, null);
        String strReturn = "";
        while (cursor.moveToNext())   
        {  
        	int iGroupColumnId = cursor.getColumnIndex(Data.DATA1);
        	int iGroupId = cursor.getInt(iGroupColumnId);
        	String strGroupId = String.valueOf(iGroupId);
//        	String strGroupName =getGroupNameById(activity, strGroupId);
        	strReturn += (strGroupId +",");
        }
        return strReturn;
    }
    
    public static String getGroupNameById(Activity activity,String strId){
    	G g = (G)activity.getApplication();
    	ArrayList<HashMap<String,Object>>  list = g.getGroup();
    	String strName = "";
		for(int i = 0; i < list.size(); i ++){
			HashMap<String,Object> map = list.get(i);
			String strMapId = map.get("Id").toString();
			if( strId ==strMapId){
				strName = map.get("Name").toString();
				break;
			}
		}
		return strName;
    }
}
